const { baseURL } = require("../../utils/request")
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    campCommunicaTionStatus: {
      type: Boolean,
      value: false
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    campCommunicaTionStatus: false,
    addList: [], //整体数据
    fileList: [], //上传图片
    userId: wx.getStorageSync("userInfo")["userId"],
    deptName: wx.getStorageSync("userInfo")["deptName"],
    content: "", // 动态内容
    imageUrlAddress: [], // 图片地址
    state: 0,
  },
  lifetimes: {
    ready() {

    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    // 绑定营地介绍
    onchangeContent(e) {
      // 将输入的值赋值给 content
      // console.log("e", e);
      this.setData({
        content: e.detail.value
      })
    },
    //重置数据
    resetData() {
      this.setData({
        content: "", // 动态内容
        fileList: [],
        userId: this.data.userId,
        deptName: this.data.deptName,
        state: 0,
      })
    },
    //添加虚拟数据
    addData(e) {
      const object = {
        content: this.data.content,
        userId: this.data.userId,
        deptName: this.data.deptName,
        imageUrlAddress: this.data.imageUrlAddress,
      }
      console.log("CampCommunicaTionList object", object);
      var isEmpty = true;
      Object.keys(object).forEach(function (x) {
        if (object[x] === null || object[x] === "") {
          isEmpty = false;
        }
      });
      if (isEmpty) {
        this.data.addList.push({
          content: this.data.content,
          userId: this.data.userId,
          deptName: this.data.deptName,
          campCommunicationImagesList: this.data.fileList,
          imageUrlAddress: this.data.imageUrlAddress
        })
        this.resetData();
        this.onClose();
        this.triggerEvent("sendData", this.data.addList)
      } else {
        wx.showToast({
          title: "信息未填写完整",
          icon: "error",
          duration: 1500
        })
      }
      // console.log(instance.globalData.campData)
      // console.log("campcommunication this.data.addList", this.data.addList);
    },
    onClose() {
      this.resetData()
      this.setData({
        campCommunicaTionStatus: false
      })
    },

    // 选择文件后的处理函数，对应van-uploader组件的after-read事件
    afterRead(event) {
      const {
        file
      } = event.detail;
      var that = this
      // 简单验证一下file是否存在，确保后续流程正常进行
      if (!file) {
        console.error('未获取到有效的文件信息');
        return;
      }
      // 将临时路径上传到服务器 多图片上传
      let token = wx.getStorageSync('userInfo').token ? wx.getStorageSync('userInfo').token : null
      const uploadTasks = []; // 用于存储所有的上传任务
      file.forEach((file)=>{
        const uploadTasks = wx.uploadFile({
          url: `${baseURL}/common/upload`, // 仅为示例，非真实的接口地址
          filePath: file.url,
          name: 'file',
          header: {
            'content-type': 'application/json',
            'Authorization': `Bearer ${token}`,
          },
          formData: {
            user: 'test'
          },
          success: (res) => {
            // 调用uploadFile方法返回的res.data数据是字符串需要使用JSON.parse()转换
            const uploadData = JSON.parse(res.data);
            // console.log("uploadData", uploadData)
            // console.log("上传成功", res)
            // 上传成功后，将返回的图片地址保存到本地
            that.setData({
              url: uploadData.fileName,
              imageUrlAddress: uploadData.fileName
            });
  
            // 回显上传图片
            var _fileList = that.data.fileList
            _fileList.push({
              // 回显上传图片
              url: uploadData.url,
              // 将上传的图片地址 赋值给 imageUrlAddress 数组
              imageUrlAddress: uploadData.fileName,
            })
            that.setData({
              fileList: _fileList
            });
            // 将图片地址保存到本地
            wx.setStorageSync('url', uploadData.url);
          }
        })
      })
    },

    //审核
    toExamine() {
      http.addCampCommunicaTion(this.data).then(res => {
        wx.showToast({
          title: '添加成功待审核',
          icon: 'success',
          duration: 1000
        })
        setTimeout(() => {
          // 返回到上一页
          wx.navigateBack({
            delta: 1,
          })
        }, 1000)
      })
    },
    // 获取动态详情
    // getProductInfo() {
    //   http.getProductDetail(this.data.productId).then(res => {
    //     this.setData({
    //       productName: res.data.productName,
    //       productPrice: res.data.productPrice,
    //       productDetails: res.data.productDetails,
    //       imageUrl: res.data.imageurl,
    //       approve: e.detail.value.approve,
    //       state: res.data.state
    //     })
    //   })
    // },
  }
})